package com.leetcode.test.array;

import org.junit.Assert;
import org.junit.Test;

public class 至少是其他数字两倍的最大数 {

	@Test
	public void test() {
		Assert.assertEquals(1, dominantIndex(new int[]{3, 6, 1, 0}));
		Assert.assertEquals(-1, dominantIndex(new int[]{1, 2, 3, 4}));
		Assert.assertEquals(3, dominantIndex(new int[]{0, 0, 0, 1}));
		Assert.assertEquals(0, dominantIndex(new int[]{1}));

	}

	public int dominantIndex(int[] nums) {
		if (nums.length < 1) {
			return -1;
		}

		int max   = -1;
		int index = -1;

		for (int i = 0; i < nums.length; i++) {
			if (nums[i] > max) {
				index = i;
				max = nums[i];
				continue;
			}
		}

		for (int i = 0; i < nums.length; i++) {
			if (i != index && max < nums[i] * 2) {
				return -1;
			}
		}

		return index;
	}
}
